/*	$Id: dlink_lpkt.c 7 2008-07-29 02:58:11Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <net/if.h>

#include <netinet/in.h>

#include <features.h>
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h>
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#ifndef SOL_PACKET
#define SOL_PACKET 263
#endif

#endif

#include <err.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "if.h"
#include "dlink.h"
#include "pforge.h"


int        dlink_lpkt_open  (PF_DLINK *);
int        dlink_lpkt_close (PF_DLINK *);
int        dlink_lpkt_bind  (PF_DLINK *, int);
int        dlink_lpkt_ctl   (PF_DLINK *, int, void *);
PF_PKT*    dlink_lpkt_read  (PF_DLINK *);
ssize_t    dlink_lpkt_write (PF_DLINK *, void *, size_t);

struct pf_dlink_ops dlink_lpkt_ops = {
	dlink_lpkt_open,
	dlink_lpkt_close,
	dlink_lpkt_bind,
	dlink_lpkt_ctl,
	dlink_lpkt_read,
	dlink_lpkt_write
};


/*
 * dlink_lpkt_open()
 *
 * Open a packet socket.
 */

int
dlink_lpkt_open(PF_DLINK *dl)
{
	int pkt_fd;

	pkt_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (pkt_fd < 0) {
		return (-1);
	}

	dl->dlfd = pkt_fd;

	return (0);
}


/*
 * dlink_lpkt_close()
 *
 * Close the packet socket opened by dlink_lpkt_open().
 */

int
dlink_lpkt_close(PF_DLINK *dl)
{
	if (dl == NULL)
		return (-1);
	(void)close(dl->dlfd);
	dl->dlfd = -1;

	return (0);
}


/*
 * dlink_lpkt_bind()
 *
 * Bind the dlink layer structure <dl> to the network interface <bif>.
 * With the packet interface, this is done by calling bind(2) on the packet
 * socket descriptor with a link level socket address.  The only fields
 * required in that structure are 'sll_protocol' and 'sll_ifindex'.
 * Returns 0 on success, or -1 on failure.
 */

int
dlink_lpkt_bind(PF_DLINK *dl, int ifid)
{
	struct sockaddr_ll sll;

	memset(&sll, 0, sizeof(sll));

	sll.sll_family = AF_PACKET;
	sll.sll_protocol = htons(ETH_P_ALL);

#if 0
	sll.sll_ifindex = bif->ifindex;
	if (bind(dl->dlfd, (struct sockaddr *)&sll, sizeof(sll)) == -1) {
		return (-1);
	}

	dl->dlif = bif;
	bif->ifdl = dl;
#endif

	return (0);
}


/*
 * dlink_lpkt_ctl()
 *
 * Linux-specific control operations on the datalink layer.
 */

int
dlink_lpkt_ctl(PF_DLINK *dl, int type, void *data)
{
	int ret = 0;
	struct timeval *tv;
	struct ifreq ifr;
	struct packet_mreq mreq;

	switch (type) {
	case PFDL_CTL_SPROMISC:
		ret = ioctl(dl->dlfd, SIOCSIFFLAGS, &ifr);
		break;
	default:
		ret = -1;
	}

	return (ret);
}


/*
 * dlink_lpkt_read()
 *
 * Read bytes from the BPF device.
 *
 */
PF_PKT*
dlink_lpkt_read(PF_DLINK *dl)
{
	int flags;
	ssize_t bytes;
	PF_PKT *pkt;

	flags = 0;

	bytes = recv(dl->dlfd, dl->rbuf, dl->rbufsz, flags);
	if (bytes == -1) {
		return (NULL);
	}

	pkt = pf_pkt_alloc();

	return (pkt);
}


/*
 * dlink_lpkt_write()
 *
 * Write a packet to the datalink layer.
 */

ssize_t
dlink_lpkt_write(PF_DLINK *dl, void *src, size_t len)
{
	ssize_t sbytes;

	sbytes = -1;

	return (sbytes);
}
